package com.dynamo.transport.network.practise.socksproxy;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author duanyimiao
 * @create 2018-11-21 5:11 PM
 * @description socks是建立tcp之上的代理协议，不想其他代理那样需要包装协议头，直接将代理的数据通过tcp传到目标对象上，无需关心发送方以什么协议传过来的 这个socks代理在client到server起到一个桥梁，因此socks相对于client就是服务端，但是相对于server端又是一个client
 **/
public class SocksProxyServer {

    public static void main(String[] args) {
        SocksProxyServer server = new SocksProxyServer();
        server.start();
    }

    public void start() {
        //boss线程池，接受请求连接的线程池
        Executor bossThreads = Executors.newCachedThreadPool();
        //work线程池，处理具体逻辑的线程池，各个handler处理逻辑就是使用该线程池中的线程
        Executor workThreads = Executors.newCachedThreadPool();

        ServerBootstrap sb = new ServerBootstrap(
                new NioServerSocketChannelFactory(bossThreads, workThreads)
        );
        //用于连接真实server端的client配置信息
        ClientSocketChannelFactory cf = new NioClientSocketChannelFactory(bossThreads, workThreads);
        // set up channel pipeline
        sb.setPipelineFactory(new SocksProxyPipelineFactory(cf));

        //start up server
        sb.bind(new InetSocketAddress(1080));
        System.out.println("startup success");
    }
}
